package com.uh.rds.tester.standalone;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.exceptions.JedisException;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisSentinelExample {
    public static void main(String[] args) {
        // Redis哨兵节点集合
        Set<String> sentinels = new HashSet<>();
        sentinels.add("127.0.0.1:26379");

        // Redis主节点名称
        String masterName = "sentinel-worker1";

        // 1. 配置连接池参数
        GenericObjectPoolConfig<Jedis> poolConfig = new GenericObjectPoolConfig<>();
        poolConfig.setMaxTotal(10);          // 最大连接数
        poolConfig.setMaxIdle(5);            // 最大空闲连接数
        poolConfig.setMinIdle(1);            // 最小空闲连接数
        poolConfig.setTestOnBorrow(true);    // 获取连接时测试有效性

        // 2. 创建哨兵连接池（直接用构造方法）
        //JedisSentinelPool sentinelPool = null;
        Jedis jedis = null;

        try {
//            sentinelPool = new JedisSentinelPool(
//                    masterName,
//                    sentinels,
//                    poolConfig,
//                    3000,
//                    null, // 密码，无密码可传 null
//                    0                // 数据库索引
//            );

//              public JedisSentinelPool(String masterName, Set<String> sentinels,
//            final GenericObjectPoolConfig<Jedis> poolConfig, final int connectionTimeout, final int soTimeout,
//            final String password, final int database, final String clientName,
//            final int sentinelConnectionTimeout, final int sentinelSoTimeout, final String sentinelPassword,
//            final String sentinelClientName) {

//            System.out.println("成功连接到Redis哨兵模式集群");
//            System.out.println("主节点: " + sentinelPool.getCurrentHostMaster());



            // 获取Jedis实例
            //jedis = sentinelPool.getResource();

            Jedis sentinelJedis = new Jedis("127.0.0.1", 26379);

//            // 获取所有从节点信息
            List<Map<String, String>> slaves = sentinelJedis.sentinelSlaves(masterName);
            for (Map<String, String> slave : slaves) {
                String ip = slave.get("ip");
                String port = slave.get("port");
                System.out.println("从节点: " + ip + ":" + port);
            }


            Map<String, String> master = sentinelJedis.sentinelMaster(masterName);
            System.out.println("主节点信息: " + master);


//            // 执行SET操作
//            String key = "example_key";
//            String value = "Hello, Redis Sentinel with Jedis 5.1.5!";
//            String setResult = jedis.set(key, value);
//            System.out.println("SET操作结果: " + setResult);
//
//            // 执行GET操作
//            String getResult = jedis.get(key);
//            System.out.println("GET操作结果: " + getResult);
//
//            // 设置过期时间
//            jedis.expire(key, 3600);
//            System.out.println("设置过期时间成功");

        } catch (JedisException e) {
            System.err.println("操作Redis时发生错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 释放资源
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    System.err.println("关闭Jedis连接时发生错误: " + e.getMessage());
                }
            }

            // 关闭连接池
            //if (sentinelPool != null) {
            //    sentinelPool.close();
            //}
        }
    }
}